/**
 * jims
 */
package com.jims.master.modules.oper.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.Encodes;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.service.ClinicItemDictService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;


/**
 * 诊疗项目Controller
 * @author 赵宁
 * @version 2017-02-23
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/clinicItemDict")
public class ClinicItemDictController extends BaseController {

	@Autowired
	private ClinicItemDictService clinicItemDictService;
	
	@ModelAttribute
	public ClinicItemDict get(@RequestParam(required=false) String id) {
		ClinicItemDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clinicItemDictService.get(id);
		}
		if (entity == null){
			entity = new ClinicItemDict();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ClinicItemDict clinicItemDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        clinicItemDict.updateOrgId();
		Page<ClinicItemDict> page = clinicItemDictService.findPage(new Page<ClinicItemDict>(request, response), clinicItemDict); 
		model.addAttribute("page", page);
		return "modules/oper/clinicItemDictIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ClinicItemDict clinicItemDict, Model model) {
		model.addAttribute("clinicItemDict", clinicItemDict);
		return "modules/oper/clinicItemDictForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ClinicItemDict clinicItemDict, Model model) {
	    StringData data=new StringData();
		int i=clinicItemDictService.saveItem(clinicItemDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存诊疗项目成功");
        }else{
            data.setCode("error");
            data.setData("保存诊疗项目失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ClinicItemDict clinicItemDict) {
	    StringData data=new StringData();
		int i=clinicItemDictService.delete(clinicItemDict);
        if(i>0){
            data.setCode("success");
            data.setData("删除诊疗项目成功");
        }else{
            data.setCode("error");
            data.setData("删除诊疗项目失败");
        }
		return data;
	}

	/**
	 * @param clinicItemDict 传递参数
	 * @return String    返回类型
	* @throws
	 * @Title: addChargeForm
	 * @Desciption: (跳转到添加对照页面)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "addChargeForm")
	public String addChargeForm(ClinicItemDict clinicItemDict, Model model) {
		/**根据诊疗项目查询所有对照**/
		List<BaseDto> list = clinicItemDictService.findVsChargeByClinicId(clinicItemDict.getId());
		model.addAttribute("itemDict", clinicItemDict);
		model.addAttribute("clinicItemDict", clinicItemDict);
		model.addAttribute("list", list);
		return "modules/oper/clinicItemDictAddVsForm";
	}

    /**
     * 诊疗项目自动补全
     * @param q
     * @param limit
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "automatic")
    @ResponseBody
    public List<ClinicItemDict> automatic(String itemClass,String q,String limit,String extId, Model model,String removeClass) {
        String orgId= UserUtils.getOrgId();
        return  clinicItemDictService.findListDict(itemClass,orgId,Encodes.urlDecode(q),limit,extId,removeClass);
    }


}